package com.polytech.web.client.entrypoint;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.polytech.web.client.service.PersonCallback;
import com.polytech.web.client.service.PersonService;
import com.polytech.web.client.service.PersonServiceAsync;
import com.polytech.web.client.service.PersonUserCallBack;
import com.polytech.web.client.table.MyTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;

public class HelloServer implements EntryPoint {

	private MyTable table;

	/**
	 * This is the entry point method.
	 */

	public void onModuleLoad() {
	    final VerticalPanel vPanel2 = new VerticalPanel();
	    

	    final Label name = new Label("Last Name :");
	    final TextBox textName = new TextBox();
	    final TextBox textLastName = new TextBox();
	    
	    final Image image = new Image("images/imfb.png");
	    name.setStyleName("centerVertical");
	    
	    final Label lblPersonne;
	    
	    final Label criteria = new Label("Criteria :");
	    criteria.setStyleName("centerVertical");
	    // or we can set an id on a specific element for styling

	    HorizontalPanel hPanel = new HorizontalPanel();
	    HorizontalPanel hPanel2 = new HorizontalPanel();
	    
	    HorizontalPanel hPanel4 = new HorizontalPanel();
	    
	    Label lblSchool = new Label("School :");
	    lblSchool.setStyleName("centerVertical");
	    hPanel4.add(lblSchool);
	    lblSchool.setWidth("72px");
	    final TextBox schoolText = new TextBox();
	    hPanel4.add(schoolText);
	    schoolText.setWidth("138px");
	    
	    
	    hPanel.add(name);
	    name.setWidth("72px");
	    hPanel.add(textName);
	    textName.setWidth("138px");
	    
	    final VerticalPanel vPanel = new VerticalPanel();
	    vPanel.setSize("716px", "413px");
	    vPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
	    
	    //hide information about the user
	    vPanel2.add(hPanel2);
	    hPanel2.setSize("727px", "37px");
	    final TextBox birthdayText = new TextBox();
	    
	    HorizontalPanel hPanel3 = new HorizontalPanel();
	    hPanel2.add(hPanel3);
	    hPanel3.setWidth("632px");
	    
	    Label lblBirthday = new Label("Birthday :");
	    lblBirthday.setStyleName("centerVertical");
	    hPanel3.add(lblBirthday);
	    lblBirthday.setWidth("72px");
	    hPanel3.add(birthdayText);
	    birthdayText.setWidth("138px");
	    
	    Label label_5 = new Label("New label");
	    hPanel3.add(label_5);
	    label_5.setWidth("14px");
	    
	    Label lblCountry = new Label("Country :");
	    lblCountry.setStyleName("centerVertical");
	    hPanel3.add(lblCountry);
	    lblCountry.setWidth("72px");
	    final TextBox countryText = new TextBox();
	    hPanel3.add(countryText);
	    countryText.setWidth("138px");
	    
	    Label label_3 = new Label("New label");
	    hPanel3.add(label_3);
	    label_3.setWidth("160px");
	    vPanel2.add(hPanel4);
	    hPanel4.setSize("717px", "39px");
	    
	    Label label_2 = new Label("New label");
	    hPanel4.add(label_2);
	    label_2.setWidth("14px");
	    
	    Label lblWork = new Label("Work :");
	    lblWork.setStyleName("centerVertical");
	    hPanel4.add(lblWork);
	    lblWork.setWidth("72px");
	    final TextBox workPlaceText = new TextBox();
	    hPanel4.add(workPlaceText);
	    workPlaceText.setWidth("138px");
	    
	    Label label_6 = new Label("New label");
	    hPanel4.add(label_6);
	    label_6.setWidth("230px");
	    
	    HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
	    vPanel2.add(horizontalPanel_1);
	    horizontalPanel_1.setSize("714px", "61px");
	    final ListBox lb = new ListBox();
	    lb.addItem("Family");
	    lb.addItem("Friend");
	    lb.addItem("Classmate");
	    lb.addItem("Colleague");
	    lb.addItem("Interest");
	    lb.addItem("Country");
	    lb.addItem("Generation");
	    lb.addItem("Groupe");
	    lb.addItem("Anniversaire");
	    lb.setStyleName("centerVertical");
	    Button search = new Button("Search");
	    
	    

	    // We can add style names
	    search.setStyleName("sendButton");
	    
	    HorizontalPanel hPanel6 = new HorizontalPanel();
	    
	    Label lblNewLabel = new Label("New label");
	    hPanel6.add(lblNewLabel);
	    lblNewLabel.setSize("59px", "34px");
	    
	    Label lblCriteria = new Label("Criteria : ");
	    lblCriteria.setStyleName("gwt-Label-new");
	    hPanel6.add(lblCriteria);
	    lblCriteria.setSize("94px", "34px");
	    hPanel6.add(lb);
	    lb.setWidth("241px");
	    hPanel6.add(search);
	    vPanel2.add(hPanel6);
	    
	    search.addClickHandler(new ClickHandler() {
	      @Override
	      public void onClick(ClickEvent event) {
	       PersonServiceAsync service = (PersonServiceAsync) GWT
	            .create(PersonService.class);
	        ServiceDefTarget serviceDef = (ServiceDefTarget) service;
	        serviceDef.setServiceEntryPoint(GWT.getModuleBaseURL()
	            + "personService");
	        PersonCallback personCallback = new PersonCallback(table);
	        int index = lb.getSelectedIndex();
	        String selectedItem = lb.getItemText(index);
	        service.getUserList(selectedItem,textName.getText(),personCallback);
	      }
	    });
	    
	    HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
	    vPanel2.add(horizontalPanel_2);
	    horizontalPanel_2.setSize("595px", "27px");
	    table = new MyTable(null);
	    vPanel2.add(table);
	    
	    HorizontalPanel horizontalPanel = new HorizontalPanel();
	    vPanel2.add(horizontalPanel);
	    horizontalPanel.setHeight("177px");
	    
	    vPanel2.setVisible(false);
	    vPanel.add(hPanel);
	    hPanel.setHeight("39px");
	    
	    Label lblNewLabel_1 = new Label("New label");
	    hPanel.add(lblNewLabel_1);
	    lblNewLabel_1.setWidth("14px");
	    
	    Label label = new Label("First Name :");
	    label.setStyleName("centerVertical");
	    hPanel.add(label);
	    label.setWidth("72px");
	    
	   // TextBox textLastName = new TextBox();
	    hPanel.add(textLastName);
	    textLastName.setWidth("138px");
	    
	    
	    Label label_1 = new Label("New label");
	    hPanel.add(label_1);
	    label_1.setWidth("20px");
	    Button connect = new Button("Connect");
	    hPanel.add(connect);
	    
	    Label lblNewLabel_2 = new Label("New label");
	    hPanel.add(lblNewLabel_2);
	    lblNewLabel_2.setWidth("171px");
	    connect.addClickHandler(new ClickHandler() {
		      @Override
		      public void onClick(ClickEvent event) {
		       PersonServiceAsync service = (PersonServiceAsync) GWT
		            .create(PersonService.class);
		        ServiceDefTarget serviceDef = (ServiceDefTarget) service;
		        serviceDef.setServiceEntryPoint(GWT.getModuleBaseURL()
		            + "personService");
		        PersonUserCallBack personCallback = new PersonUserCallBack(vPanel2,birthdayText,countryText,workPlaceText, image, schoolText);
		        String userName = textName.getText();
		        String userlastName=textLastName.getText();
		        if(userName.equals("") || userlastName.equals("") ){
		        	//alert
		        	Window.alert("You have to log !!");
		        }else{
		        service.getUser(userName,userlastName, personCallback);
		        }
		      }
		    });
	    vPanel.add(vPanel2);
	    vPanel2.setHeight("334px");

	    // Add table and button to the RootPanel
	    RootPanel rootPanel = RootPanel.get();
	    rootPanel.add(vPanel, 432, 119);
	    
	    VerticalPanel verticalPanel = new VerticalPanel();
	    rootPanel.add(verticalPanel, 197, 107);
	    verticalPanel.setSize("229px", "172px");
	    
	    
	    verticalPanel.add(image);
	    image.setSize("218px", "168px");
	    
	    HorizontalPanel horizontalPanel_4 = new HorizontalPanel();
	    verticalPanel.add(horizontalPanel_4);
	    horizontalPanel_4.setSize("153px", "40px");
	    
	    VerticalPanel verticalPanel_1 = new VerticalPanel();
	    verticalPanel.add(verticalPanel_1);
	    verticalPanel_1.setSize("194px", "96px");
	    
	    HorizontalPanel horizontalPanel_5 = new HorizontalPanel();
	    verticalPanel_1.add(horizontalPanel_5);
	    horizontalPanel_5.setHeight("28px");
	    
	    Label lblFriendSuggestion = new Label("Friend Suggestion");
	    horizontalPanel_5.add(lblFriendSuggestion);
	    lblFriendSuggestion.setStyleName("gwt-Label-new");
	    
	    HorizontalPanel horizontalPanel_6 = new HorizontalPanel();
	    verticalPanel_1.add(horizontalPanel_6);
	    horizontalPanel_6.setSize("218px", "31px");
	    
	    lblPersonne = new Label("Jennifer BOURDIN");
	    horizontalPanel_6.add(lblPersonne);
	    lblPersonne.setWidth("120px");
	    lblPersonne.setStyleName("gwt-Label-new2");
	    
	    Button addFriend = new Button("New button");
	    horizontalPanel_6.add(addFriend);
	    addFriend.setWidth("90px");
	    addFriend.setText("Add Friend");
	    
	    HorizontalPanel horizontalPanel_7 = new HorizontalPanel();
	    verticalPanel_1.add(horizontalPanel_7);
	    horizontalPanel_7.setSize("147px", "16px");
	    
	    HorizontalPanel horizontalPanel_3 = new HorizontalPanel();
	    verticalPanel_1.add(horizontalPanel_3);
	    horizontalPanel_3.setWidth("216px");
	    
	    Label label_4 = new Label("Pierre MORINO");
	    label_4.setStyleName("gwt-Label-new2");
	    horizontalPanel_3.add(label_4);
	    label_4.setWidth("120px");
	    
	    Button addFriend1 = new Button("New button");
	    addFriend1.setText("Add Friend");
	    horizontalPanel_3.add(addFriend1);
	    addFriend1.setWidth("90px");
	    
	    addFriend1.addClickHandler(new ClickHandler() {
		      @Override
		      public void onClick(ClickEvent event) {
		       PersonServiceAsync service = (PersonServiceAsync) GWT
		            .create(PersonService.class);
		        ServiceDefTarget serviceDef = (ServiceDefTarget) service;
		        serviceDef.setServiceEntryPoint(GWT.getModuleBaseURL()
		            + "personService");
		        PersonUserCallBack personCallback = new PersonUserCallBack(vPanel2,birthdayText,countryText,workPlaceText, image, schoolText);
		        String userNameF = lblPersonne.getText();
		        String userNameFriend=userNameF.substring(0,userNameF.indexOf(" "));
		        System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+userNameFriend);
		        String username=textName.getText();
		        System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+username);
		        if(userNameF.equals("")){
		        	//alert
		        	Window.alert("You have to log !!");
		        }else{
		        service.addFriend(username,userNameFriend, personCallback);
		        }
		      }
		    });
	    
	    addFriend.addClickHandler(new ClickHandler() {
		      @Override
		      public void onClick(ClickEvent event) {
		       PersonServiceAsync service = (PersonServiceAsync) GWT
		            .create(PersonService.class);
		        ServiceDefTarget serviceDef = (ServiceDefTarget) service;
		        serviceDef.setServiceEntryPoint(GWT.getModuleBaseURL()
		            + "personService");
		        PersonUserCallBack personCallback = new PersonUserCallBack(vPanel2,birthdayText,countryText,workPlaceText, image, schoolText);
		        String userNameF = lblPersonne.getText();
		        String userNameFriend=userNameF.substring(0,userNameF.indexOf(" "));
		        System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+userNameFriend);
		        String username=textName.getText();
		        System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+username);
		        if(userNameF.equals("")){
		        	//alert
		        	Window.alert("You have to log !!");
		        }else{
		        service.addFriend(username,userNameFriend, personCallback);
		        }
		      }
		    });
}
}